package com.lenovo.manufacture.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import com.beardedhen.androidbootstrap.BootstrapButton;
import com.beardedhen.androidbootstrap.BootstrapEditText;
import com.beardedhen.androidbootstrap.BootstrapLabel;
import com.beardedhen.androidbootstrap.BootstrapText;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.lenovo.manufacture.BaseActivity;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.module.Module11;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Fragment11_1 extends Fragment {

    @BindView(R.id.rb_11)
    RadioButton rb11;
    @BindView(R.id.rb_12)
    RadioButton rb12;
    @BindView(R.id.rb_13)
    RadioButton rb13;
    @BindView(R.id.rg_1)
    RadioGroup rg1;
    @BindView(R.id.btn_selectTime)
    BootstrapButton btnSelectTime;
    @BindView(R.id.et_num)
    BootstrapEditText etNum;
    @BindView(R.id.btn_submit)
    BootstrapButton btnSubmit;
    @BindView(R.id.rg_2)
    RadioGroup rg2;
    @BindView(R.id.rg_3)
    RadioGroup rg3;
    @BindView(R.id.rg_4)
    RadioGroup rg4;
    @BindView(R.id.rg_5)
    RadioGroup rg5;

    BaseActivity baseActivity;
    @BindView(R.id.sp_engine)
    Spinner spEngine;
    @BindView(R.id.sp_hang)
    Spinner spHang;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_module11_1, container, false);
        ButterKnife.bind(this, view);
        baseActivity = ((BaseActivity) getActivity());
        initSpinner();
        return view;
    }

    private void initSpinner() {
        spEngine.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                baseActivity.json.put("engine", position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        spHang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                baseActivity.json.put("hang", position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    @OnClick({R.id.btn_selectTime, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_selectTime:
                initCalendarView();
                break;
            case R.id.btn_submit:
                submit();
        }
    }

    private void submit() {
        Map<String, Object> json = baseActivity.json;
        String s = etNum.getText().toString();
        if (TextUtils.isEmpty(s)) {
            baseActivity.showMsg("请输入数量");
            return;
        }
        json.put("num", s);
        if (json.get("time") == null) {
            baseActivity.showMsg("请选择交付时间");
            return;
        }
        int carId = 0;
        switch (rg1.getCheckedRadioButtonId()) {
            case R.id.rb_11:
                break;
            case R.id.rb_12:
                carId = 1;
                break;
            case R.id.rb_13:
                carId = 2;
                break;
        }
        json.put("carId", carId);
        int speed = rg2.getCheckedRadioButtonId() == R.id.rb_21 ? 0 : 1;
        json.put("speed", speed);
        int brake = rg5.getCheckedRadioButtonId() == R.id.rb_51 ? 0 : 1;
        json.put("brake", brake);
        int wheel = rg3.getCheckedRadioButtonId() == R.id.rb_31 ? 0 : 1;
        json.put("wheel", wheel);
        int control = rg3.getCheckedRadioButtonId() == R.id.rb_41 ? 0 : 1;
        json.put("control", control);

        if (json.get("engine") == null) json.put("engine", 0);
        if (json.get("hang") == null) json.put("hang", 0);

        baseActivity.async(baseActivity.apiService.createOrder(json), data -> {
            baseActivity.showMsg("提交订单成功");

            ((Module11) requireActivity()).fragment11_2.init();
        });
    }

    @SuppressLint("DefaultLocale")
    private void initCalendarView() {
        CalendarView calendarView = new CalendarView(getContext());
        AlertDialog show = new AlertDialog.Builder(getContext())
                .setView(calendarView)
                .setTitle(String.format("请选择交付时间 %d年%d月%d日", calendarView.getCurYear(), calendarView.getCurMonth(), calendarView.getCurDay()))
                .setNegativeButton("确定",null)
                .show();
        calendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {
            @Override
            public void onCalendarOutOfRange(Calendar calendar) {

            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                BootstrapText bootstrapText = new BootstrapText.Builder(getContext())
                        .addText(String.format("%d年%d月%d日", calendar.getYear(), calendar.getMonth(), calendar.getDay()))
                        .addMaterialIcon("md_arrow_drop_down")
                        .build();
                btnSelectTime.setBootstrapText(bootstrapText);
                show.setTitle(String.format("请选择交付时间 %d年%d月%d日", calendar.getYear(), calendar.getMonth(), calendar.getDay()));
                baseActivity.json.put("time", ((int) (calendar.getTimeInMillis() / 1000)));
            }
        });
    }

}
